var chooseLocation = requirePlugin('chooseLocation');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods_list: [],
    request_num: 0,
    curpage: 1,
    cat_id: '',
    content: '',
    zh: true,//综合排序
    xl: false,//销量排序
    jg: false,//价格排序
    actorder: 'DESC',
    show: 0,
    is_show_time: 1,
	locationName: '',
	// 分类数据
	categories: [],
	currentCategory: 0, // 当前选中的分类
	scrollLeft: 0, // 滚动位置
  },
// 分类点击事件
  onCategoryTap: function(e) {
    const categoryId = e.currentTarget.dataset.id;
    this.setData({
      currentCategory: categoryId
    });
    
    // 根据分类ID加载对应商品
    this.loadGoodsByCategory(categoryId);
  },

  // 滚动事件
  onScroll: function(e) {
	  console.log(e.detail.scrollLeft)
    // this.setData({
    //   scrollLeft: e.detail.scrollLeft
    // });
  },
  // 根据分类加载商品
    loadGoodsByCategory: function(categoryId) {
      var that = this;
      // 这里实现根据分类ID加载商品的逻辑
      console.log('加载分类ID为', categoryId, '的商品');
      
      var url = app.configData.shop.api_url + '/index.php?ctl=Goods_Goods&met=goodslist&typ=json&ua=wap&pagesize=10'
      var data = { cat_id: categoryId, curpage: 1, type_wxapp: 'wxapp',locationName:that.data.locationName }
      
      
      wx.request({
        url: url,
        method: 'GET',
        data: data,
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          that.setData({
            goods_list: res.data.data.items,
            cat_id: categoryId,
			is_show_time: 0,
            curpage: 1,
            request_num: 0,
			currentCategory: categoryId
          });
      
        }
      })
      
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var content = options.content
    var cat_id = options.cat_id
	
	console.log(app.globalData)
	if (app.globalData.selectedLocation) {
	  that.setData({ locationName: app.globalData.selectedLocation.name });
	}
    if (content == undefined) {
      content = ''
    }
    if (cat_id == undefined) {
      cat_id = ''
    }

    var url = app.configData.shop.api_url + '/index.php?ctl=Goods_Goods&met=goodslist&typ=json&ua=wap&pagesize=10'
    if (cat_id != '') {
      var data = { cat_id: cat_id, curpage: that.data.curpage, type_wxapp: 'wxapp',locationName:that.data.locationName }
    } else {
      var data = { keywords: content, curpage: that.data.curpage, type_wxapp: 'wxapp',locationName:that.data.locationName }
    }

    wx.request({
      url: url,
      method: 'GET',
      data: data,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          goods_list: res.data.data.items,
          cat_id: cat_id,
          content: content,
          is_show_time: 0,
		  categories: res.data.data.cat_list,
		  currentCategory: cat_id
        });

      }
    })

  },


  //综合排序
  zh: function () {
    var that = this
    var url = app.configData.shop.api_url + '/index.php?ctl=Goods_Goods&met=goodslist&typ=json&ua=wap&pagesize=10'
    var data = { type_wxapp: 'wxapp', cat_id: that.data.cat_id, keywords: that.data.content,locationName:that.data.locationName }
    wx.request({
      url: url,
      method: 'GET',
      data: data,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          goods_list: res.data.data.items,
          curpage: 1,
          request_num: 0,
          zh: true,
          xl: false,
          jg: false
        });

      }
    })
  },
  //销量排序
  xl: function () {
    var that = this
    var url = app.configData.shop.api_url + '/index.php?ctl=Goods_Goods&met=goodslist&typ=json&ua=wap&pagesize=10'
    var data = { actorder: 'DESC', act: 'sale', type_wxapp: 'wxapp', cat_id: that.data.cat_id, keywords: that.data.content,locationName:that.data.locationName }
    wx.request({
      url: url,
      method: 'GET',
      data: data,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          goods_list: res.data.data.items,
          curpage: 1,
          request_num: 0,
          zh: false,
          xl: true,
          jg: false
        });

      }
    })
  },
  //价格排序
  jg: function () {
    var that = this
    var actorder = that.data.actorder
    var url = app.configData.shop.api_url + '/index.php?ctl=Goods_Goods&met=goodslist&typ=json&ua=wap&pagesize=10'
    if (that.data.actorder == 'DESC') {
      actorder = 'ASC'
    } else {
      actorder = 'DESC'
    }
    var data = { actorder: actorder, act: 'price', type_wxapp: 'wxapp', cat_id: that.data.cat_id, keywords: that.data.content,locationName:that.data.locationName }
    wx.request({
      url: url,
      method: 'GET',
      data: data,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          goods_list: res.data.data.items,
          curpage: 1,
          request_num: 0,
          zh: false,
          xl: false,
          jg: true,
          actorder: actorder
        });

      }
    })
  },
  //跳转至搜索页
  search: function () {
    wx.redirectTo({
      url: '../search/search'
    })
  },
  //跳转至商品详情页
  goProduct: function (e) {
    var goods_id = e.currentTarget.dataset.goods_id
    wx.redirectTo({
      url: '../product_detail/product_detail?goods_id=' + goods_id
    })
  },

  /**
     * 页面上拉触底事件的处理函数
     */
  onReachBottom: function () {
    var that = this
    var curpage = that.data.curpage
    var datas = ''
    if (that.data.request_num == 1) {
      return false
    }
    curpage++
    if (that.data.cat_id != '') {
      if (that.data.xl == true) {
        datas = { cat_id: that.data.cat_id, curpage: curpage, actorder: 'DESC', act: 'sale', type_wxapp: 'wxapp',locationName:that.data.locationName }
      } else if (that.data.jg == true) {
        if (that.data.actorder == 'DESC') {
          datas = { cat_id: that.data.cat_id, curpage: curpage, actorder: 'DESC', act: 'price', type_wxapp: 'wxapp',locationName:that.data.locationName }
        } else {
          datas = { cat_id: that.data.cat_id, curpage: curpage, actorder: 'ASC', act: 'price', type_wxapp: 'wxapp',locationName:that.data.locationName }
        }
      } else {
        datas = { cat_id: that.data.cat_id, keywords: that.data.content, curpage: curpage, type_wxapp: 'wxapp',locationName:that.data.locationName }
      }
    } else {
      if (that.data.xl == true) {
        datas = { keywords: that.data.content, curpage: curpage, actorder: 'DESC', act: 'sale', type_wxapp: 'wxapp',locationName:that.data.locationName }
      } else if (that.data.jg == true) {
        if (that.data.actorder == 'DESC') {
          datas = { keywords: that.data.content, curpage: curpage, actorder: 'DESC', act: 'price', type_wxapp: 'wxapp',locationName:that.data.locationName }
        } else {
          datas = { keywords: that.data.content, curpage: curpage, actorder: 'ASC', act: 'price', type_wxapp: 'wxapp',locationName:that.data.locationName }
        }
      } else {
        datas = { keywords: that.data.content, curpage: curpage, type_wxapp: 'wxapp',locationName:that.data.locationName }
      }

    }

    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Goods_Goods&met=goodslist&typ=json&ua=wap&pagesize=10',
      method: 'GET',
      data: datas,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {

        if (res.data.data.items.length > 0) {
          var goods_list = that.data.goods_list
          for (var z in res.data.data.items) {
            goods_list.push(res.data.data.items[z])
          }
          that.setData({
            goods_list: goods_list,
            curpage: curpage
          })
        } else {
          that.setData({
            request_num: 1
          })
        }
      }
    })
  },
  bindhref: function (e) {
    app.goTop()
    this.setData({
      show: 0
    })
  },
  //一键到顶
  onPageScroll: function (e) {
    // 获取滚动条当前位置
    var that = this
    wx.getSystemInfo({
      success: function (res) {

        if (e.scrollTop + res.windowHeight > res.windowHeight) {
          that.setData({
            show: 1
          })
        } else {
          that.setData({
            show: 0
          })
        }
      },
    })
  },
})